#include <globalDecl.h>
#include <mainDriver.h>
#include <file.h>
#include <menu.h>
#include <schema.h>
#include <attribute.h>
#include <record.h>
#include <page.h>

int main(void)
{
	/**************************************
	Declarations: 
	**************************************/ 
	menu oMain;
	int iChoice;
	/**************************************
	Show menu and aceept option.
	**************************************/ 
	try{
	/*
	char src[20]="Sing me a song";
	src[8]='\0';

	char* c = new char[20];
  
	Attribute attr(String);
	attr.SetData(src);
	attr.GetData();

	attr.ToBinary(c);
  
	Attribute backAtt(String);

	backAtt.FromBinary(c);
	backAtt.GetData();
  
	delete [] c;
	*/
	oMain.showMenu();
	oMain.acceptChoice();
	iChoice = oMain.getChoice(); 
  
	File F;	
	Page P(PAGESIZE);
	Record R;
	switch(iChoice)
	{
		case 1:
			char txtName[100];
			char binPath[100];
			char schName[100];
			char relName[100];
			
			cout << "What is the name of the text file to load?";
			cin  >> txtName;
	
			cout << "What is the name of the file where the schema is located?";
			cin  >> schName;
	
			cout << "What is the name of the relation schema?";
			cin  >> relName;

			cout << "Where should the binary file be stored?";
			cin  >> binPath;
			
			F.setParams(txtName,schName,relName,binPath);
			F.txt2bin();
			break;
		case 2:
			char schemaFile[100];
			char relSchema[100];
			char predName[100];
			char binName[100];
			
			cout << "What is the name of the file where the schema is located?";
			cin  >> schemaFile;
		
			cout << "What is the name of the relation schema?";
			cin  >> relSchema;
		
			cout << "What is the name of the file where the selection predicate is stored?";
			cin  >> predName;
		
			cout << "What is the name of the binary data file?";
			cin  >> binName;
		
			F.ApplyPredicate(schemaFile, relSchema, predName, binName); 
			break;
		default:
			cout<< "Wrong choice! needs to be [1 or 2]"<<endl;
			break;
	} 
/*   
Schema xx(relName,schName); 
F.Open(1, binPath);

FILE * pF;
pF = fopen ("orders","r");

int ret22 = 1;

while(ret22 == 1)
{
	Record rec22;
	ret22 = rec22.SuckNextRecord(xx,pF);
	if(ret22 == 1)
		F.Append(rec22);
}
*/

/*
F.GetPage(P,1);

P.GetFirst(R);
R.Print(xx);
  
P.GetFirst(R);
R.Print(xx);
P.GetFirst(R);
R.Print(xx);
*/
/*
cout << "GetLen::"<<F.GetLength()<<endl;
F.MovePointer(1);
/*
int ret =1;
	for(int i=0; i< 1000; i++)
	{

		ret = P.GetFirst(R);
		if(ret == 0)
		{
			cout<< "Page Empty";
			break;
		}
		else
		if(i%100 == 0)
			R.Print(xx);
	}		
*/
/*
	int ret =1,counter =0;
	while (ret == 1)
	{
		Record R3;
		ret = F.NextRec(R3);
		if(ret == 1)
			R3.Print(xx);
		counter++;
	}

	cout<< "Rec Count = "<< counter<<endl;
*/		
  }
  catch(int i)
  {
    cout<<"what the fuck !!"<<i;
  }
  catch(...)
  {
   cout<<"default exception!";

  }
/*
Schema xx("nation","myFile");
cout << xx.Find("n_regionkey") << " " << xx.Find("n_regionkey") << endl;
cout << xx.GetType(1) << " " << xx.GetType("n_nationkey") << endl;
char * name;
xx.GetName(name, 1);
cout << name << endl;
cout << endl;
xx.Print(); 
Schema yy(xx);
yy.Print();*/




//Testing Record//
/*
Schema xx("orders","myFile");
FILE * pFile;
pFile = fopen ("orders","r");

int ret = 4;
Record rec;
ret = rec.SuckNextRecord(xx,pFile);
cout << "Return Suck: " << ret << endl;
rec.Print(xx);
cout << "How Many Bytes: " << rec.HowManyBytes() << endl;
cout << endl;

char * newPage = new char[PAGESIZE];
rec.ToBinary(newPage);
Record rec1;
rec1.FromBinary(newPage);
rec1.Print(xx);
cout << "How Many Bytes: " << rec1.HowManyBytes() << endl;
cout << endl;

ret = rec1.SuckNextRecord(xx,pFile);
rec1.Print(xx);
cout << "Return Suck: " << ret << endl;
cout << "How Many Bytes: " << rec1.HowManyBytes() << endl;
cout << endl;

rec1.ToBinary(newPage);
Record rec2;
rec2.FromBinary(newPage);
rec2.Print(xx);
cout << "How Many Bytes: " << rec2.HowManyBytes() << endl;
cout << endl;

Record recConsume;
recConsume.Copy(rec2);
recConsume.Print(xx);
cout << "How Many Bytes: " << recConsume.HowManyBytes() << endl;
cout << endl;

while(ret == 1)
{
	Record recSuck;
	ret = recSuck.SuckNextRecord(xx,pFile);
	if(ret == 1)
	{
			recSuck.Print(xx);
			cout << "How Many Bytes: " << recSuck.HowManyBytes() << endl;
	}
	cout << "Return Suck: " << ret << endl;
	cout << endl;
}
fclose(pFile);
*/
/*
File F;
F.var =100;
cout << F.GetLength();
*/
//TEST PAGE
/*
Schema xx("orders","myFile");
FILE * pFile;
pFile = fopen ("orders","r");
char * newPage = new char[PAGESIZE];

int ret = 4;
Record rec;
ret = rec.SuckNextRecord(xx,pFile);

Record rec1;
ret = rec1.SuckNextRecord(xx,pFile);

Record recSuck;
ret = recSuck.SuckNextRecord(xx,pFile);

Page pg(PAGESIZE);
ret = 1;
int count = 0;
while (ret == 1)
{
	Record rec3;
	rec3.Copy(rec);
	ret = pg.Append(rec3);
	if(ret == 1)
	{
		count ++;
	}
	//pg.Append(rec1);
	//pg.Append(recSuck);
}
cout << "Count Load: "<< count << endl;
cout << "ToBinary: " << pg.ToBinary(newPage) << endl;
Page pg1(PAGESIZE);
cout << "FromBinary: " << pg1.FromBinary(newPage) << endl;
int get = 1;
count = 0;
while(get == 1)
{
	Record reco;
	get = pg1.GetFirst(reco);
	if(get == 1)
	{
		//reco.Print(xx);
		count++;
	}
}	
cout << "Count Get: "<< count << endl;
fclose(pFile);
*/
return 0;
}

